require "Common/define"
require "Logic/CtrlManager"
require "Logic/Config"
require "Net/roleListHandler"

G = G or {};
mapHandler = mapHandler or {};
local sub_type_mapInfo = Config.sub_type_mapInfo
local routesPath = {};
local targetMapId;
local tempMapData;
local spawntimer;


function mapHandler.changeMapById( mapid )

    local data = {};

    data["coordx"]= 1500
    data["coordy"]= 300
    data["toward"]=0
    data["map_id"]=mapid;
    data["binFile"]= G.dataTable["T_map"][tostring(mapid)].f_MapUse;
    data["name"]= G.dataTable["T_map"][tostring(mapid)].f_MapName;
    data["mapTip"]="";
    data["race"]= 1;
    data["type"]= 1;
    data["mode"]=1;
    data["chestRefresh"]=0;
    data["fileLenth"]=0;
    data["nameImg"]=0;
    data["nameImg"]=""--去掉这个字节数组，已经不需要了，存在会报错

    --相连地图
    data["exitMapNum"]=0;
    --MapInfo
    data["exitMapList"]={};
    for i=1,data["exitMapNum"] do

    end

    --npc
    data["npcNum"] = 0;
    data["npcList"] = {}
    for i=1,data["npcNum"] do
        local npc = {}
        npc["npcx"] = 0;
        npc["npcy"] = 0;
        npc["NpcShow"] = "";
        table.insert(data["npcList"],npc);
    end

    --怪物
    data["monsterNum"] = 1;
    data["monsterList"] = {};
    for i=1,data["monsterNum"] do
        local monster = {}
        monster["monsterx"] = 15;
        monster["monstery"] = 1;
        monster["monId"] = 1016;
        monster["exclusiveId"] = 1;
        monster["isDead"] = 0;
        table.insert(data["monsterList"],monster);
    end

    --物品
    data["itemNum"]=0;
    data["itemList"]={}
    for i=1,data["itemNum"] do
        local item = {}
        item["itemx"] = WebMsgMgr:readByte(p)
        item["itemy"] = WebMsgMgr:readByte(p)
        item["itemName"] = WebMsgMgr:readString(p)
        item["itemDesc"] = WebMsgMgr:readString(p)
        item["itemAction"] = WebMsgMgr:readString(p)

        item["isExist"] = WebMsgMgr:readByte(p)
        item["canAction"] = WebMsgMgr:readByte(p)
        item["canDisappear"] = WebMsgMgr:readByte(p)

        item["imgFile"] = WebMsgMgr:readString(p)

        item["fileLenth"] = WebMsgMgr:readShort(p)
        item["nameImg"] = WebMsgMgr:readBytes(p,item["fileLenth"])
        item["nameImg"] = ""--不需要字节数组
        table.insert(data["itemList"],item);
    end

    --if mapMgr.CurrentMap == nil then
    --    data["first"] = 1;
    --end

    local result = getJsonStr(data);
    --  mapMgr:requestMonsterInfo(result);
    local mapChange = Game.curMapId ~= data["map_id"];
    data["mapChange"] = mapChange and 1 or 0;
    G.serverData["mapInfo"] = data;

    mapId = tostring(data["map_id"]);
	mapInfo = G.dataTable["T_map"][mapId];
	if mapInfo ~= nil then
        local mapName = mapInfo.f_MapUse;
		resMgr:LoadMapPrefab("Prefabs/MapPref/"..mapName, function(map)
		end);
	end

end

function mapHandler.getNextMapName(point)

    if(G.serverData["mapInfo"] ~= nil) then
        local exitmap = G.serverData["mapInfo"].exitMapList;
        for i=1,#exitmap do
            local exitpoint = exitmap[i].exitList;
            for j=1,#exitpoint do
                if exitpoint[j].exitx == tonumber(point[1]) and exitpoint[j].exity == tonumber(point[2]) then

                    local mapname = "<color=#fff06d>"..exitmap[i].toMapName;
                    if(exitmap[i].isExistOwner == 1) then
                        mapname = mapname..string.format("(%s)",exitmap[i].ownerState);
                    end
                    mapname = mapname.."</color>";
                    return mapname;
                end
            end
        end

    end
    return "";

end

function mapHandler.getTaskNextMapId(destmapId)

    routesPath = {};
    targetMapId = destmapId;

    local function getNextRoute(routes,routenum)

        if indexofTable(routes,routenum) < 0 then
            table.insert(routes,routenum);
            if routenum == targetMapId then
                table.insert(routesPath,routes);
                --printTable(routes);
            end
        else return;
        end
        local worldmap = G.dataTable["WorldMap"];
        if worldmap[tostring(routenum)].PathMap ~= nil and worldmap[tostring(routenum)].PathMap ~= "" then
            local neibs = string_split(worldmap[tostring(routenum)].PathMap,",");
            for j=1,#neibs do
                local temproute = {};
                for i=1,#routes do
                    table.insert(temproute,routes[i]);
                end
                getNextRoute(temproute,tonumber(neibs[j]));
            end
        end
    end

    local mapid = G.serverData["mapInfo"].map_id;
    getNextRoute({},mapid);

    local shortestPath;
    if(#routesPath > 0) then
        shortestPath = routesPath[1];
    end
    for i=2,#routesPath do
        if #routesPath[i] < #shortestPath then
            shortestPath = routesPath[i];
        end
    end
    --print("最短路径");
    --printTable(shortestPath);

    return shortestPath;
end

